﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
namespace TotalCrytography.Data
{
    public class UserDAO
    {

        #region 1. Inserting
        public void Insert(UserDTO info)
        {
            DataProvider.Connect();
            string insertCommand = "INSERT INTO UserData VALUES(@Email,@Pass,@Name,@Birthday,@Number,@Address,@Salt,@PublicKey,@PrivateKey)";
            DataProvider.m_Command = new OleDbCommand(insertCommand, DataProvider.m_Connection);
            DataProvider.m_Command.Parameters.AddWithValue("@Email", info.Email);
            DataProvider.m_Command.Parameters.AddWithValue("@Pass", info.HashPass);
            DataProvider.m_Command.Parameters.AddWithValue("@Name", info.Name);
            DataProvider.m_Command.Parameters.AddWithValue("@Birthday", info.Birthday);
            DataProvider.m_Command.Parameters.AddWithValue("@Phone", info.Phone);
            DataProvider.m_Command.Parameters.AddWithValue("@Address", info.Address);
            DataProvider.m_Command.Parameters.AddWithValue("@Salt", info.Salt);
            DataProvider.m_Command.Parameters.AddWithValue("@PublicKey", info.PublicKey);
            DataProvider.m_Command.Parameters.AddWithValue("@PrivateKey", info.PrivateKey);
            DataProvider.m_Command.ExecuteNonQuery();
            DataProvider.Disconnect();
        }
        #endregion

        #region 2. Updating
        public void Update(UserDTO info)
        {
            DataProvider.Connect();
            string updateCommand = "UPDATE UserData SET Pass = @Pass, Name = @Name, Birthday = @Birthday, Phone = @Phone,Address = @Address,PrivateKey = @PrivateKey WHERE Email = @Email";
            DataProvider.m_Command = new OleDbCommand(updateCommand, DataProvider.m_Connection);
            DataProvider.m_Command.Parameters.AddWithValue("@Pass", info.HashPass);
            DataProvider.m_Command.Parameters.AddWithValue("@Name", info.Name);
            DataProvider.m_Command.Parameters.AddWithValue("@Birthday", info.Birthday);
            DataProvider.m_Command.Parameters.AddWithValue("@Phone", info.Phone);
            DataProvider.m_Command.Parameters.AddWithValue("@Address", info.Address);
            DataProvider.m_Command.Parameters.AddWithValue("@PrivateKey", info.PrivateKey);
            DataProvider.m_Command.Parameters.AddWithValue("@Email", info.Email);
            DataProvider.m_Command.ExecuteNonQuery();
            DataProvider.Disconnect();
        }
        #endregion

        #region 3. Retrieving
        public UserDTO GetUser(string strEmail)
        {
            DataProvider.Connect();
            string getUserCommand = "Select * FROM UserData Where Email = @Email ";
            DataProvider.m_Command = new OleDbCommand(getUserCommand, DataProvider.m_Connection);
            DataProvider.m_Command.Parameters.AddWithValue("@Email", strEmail);
            OleDbDataReader data = (OleDbDataReader)DataProvider.m_Command.ExecuteReader();
            UserDTO userDto = null;
            if (data.HasRows)
            {
                data.Read();
                userDto = new UserDTO();
                userDto.Email = data.GetString(0);
                userDto.HashPass = data.GetString(1);
                userDto.Name = data.GetString(2);
                userDto.Birthday = data.GetString(3);
                userDto.Phone = data.GetString(4);
                userDto.Address = data.GetString(5);
                userDto.Salt = data.GetString(6);
                userDto.PublicKey = data.GetString(7);
                userDto.PrivateKey = data.GetString(8);
            }
            DataProvider.Disconnect();
            return userDto;
        }

        public ArrayList SelectAll()
        {
            DataProvider.Connect();
            string query = "SELECT * FROM UserData";
            DataProvider.m_Adapter = new OleDbDataAdapter(query, DataProvider.m_Connection);
            DataSet dataset = new DataSet();
            DataProvider.m_Adapter.Fill(dataset);
            ArrayList arr = ConvertDataSetToArrayList(dataset);
            DataProvider.Disconnect();
            return arr;
        }

        ArrayList ConvertDataSetToArrayList(DataSet dataset)
        {
            ArrayList arr = new ArrayList();
            DataTable dt = dataset.Tables[0];
            int i, n = dt.Rows.Count;
            for (i = 0; i < n; i++)
            {
                object obj = GetDataFromDataRow(dt, i);
                arr.Add(obj);
            }
            return arr;
        }

        object GetDataFromDataRow(DataTable dt, int i)
        {
            UserDTO userDto = new UserDTO();
            userDto.Email = dt.Rows[i]["Email"].ToString();
            userDto.HashPass = dt.Rows[i]["Pass"].ToString();
            userDto.Name = dt.Rows[i]["Name"].ToString();
            userDto.Birthday = dt.Rows[i]["Birthday"].ToString();
            userDto.Phone = dt.Rows[i]["Phone"].ToString();
            userDto.Address = dt.Rows[i]["Address"].ToString();            
            userDto.Salt = dt.Rows[i]["Salt"].ToString();
            userDto.PublicKey = dt.Rows[i]["PublicKey"].ToString();
            userDto.PrivateKey = dt.Rows[i]["PrivateKey"].ToString();
            return (object)userDto;
        }
        #endregion

        #region 4. Checking
        public Boolean IsExisted(string strEmail)
        {
            UserDTO userDto = GetUser(strEmail);
            if (userDto != null)
                return true;
            return false;
        }
        #endregion
    }
}
